﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace HY.BLL
{
   public class BllSales
    {


       /// <summary>
       /// 获取当前系统设定
       /// </summary>
       /// <param name="sy"></param>
       /// <returns></returns>
       public static string[] getsalesnum(string sy)
       {
           String[] strArr = new String[4]; 

           DataTable de = BLL.Bllbase.get_table("sales_set","salestype='"+sy+"'");

           if (de.Rows.Count > 0)
           {
               strArr[0] = de.Rows[0]["maxnum"].ToString(); //最大数
               strArr[1] = de.Rows[0]["maxdate"].ToString();
           }
           return strArr;
       }

      /// <summary>
      /// 删除收藏记录
      /// </summary>
      /// <param name="cbhlist">企业编号</param>
       public static void deletelistsalse(string tabtype,string cbhlist)
       { 
           DataTable dt=null;
            string sqltxt = "";
            string val = "";

            if (tabtype == "company")
            {
                dt = BLL.Bllbase.get_table("company", " sid in (" + cbhlist + ")");
                val = "cbh";
            }

            if (tabtype == "school")
            {
                dt = BLL.Bllbase.get_table("school", " sid in (" + cbhlist + ")");
                val = "schbh";

            }
            if (tabtype == "zj")
            {
                dt = BLL.Bllbase.get_table("zj", " sid in (" + cbhlist + ")");
                val = "zbh";
            }

               for (int i = 0; i < dt.Rows.Count; i++)
               {
                       
                 sqltxt = "delete company_fav where cbh ='" +  dt.Rows[i][val].ToString().Trim() + "' ";

                 DAL.DbHelperSQL.ExecuteNonQuery(sqltxt);
               }
       
       }

       /// <summary>
       /// 删除记录
       /// </summary>
       /// <param name="cbhlist">企业编号</param>
       public static void deletelistsalse2(string tabtype, string cbhlist)
       {
           DataTable dt = null;
           string sqltxt = "";
           string val = "";

           if (tabtype == "company")
           {
               dt = BLL.Bllbase.get_table("company", " sid in (" + cbhlist + ")");
               val = "cbh";
           }

           if (tabtype == "school")
           {
               dt = BLL.Bllbase.get_table("school", " sid in (" + cbhlist + ")");
               val = "schbh";
           }

           if (tabtype == "zj")
           {
               dt = BLL.Bllbase.get_table("zj", " sid in (" + cbhlist + ")");
               val = "zbh";
           }

           for (int i = 0; i < dt.Rows.Count; i++)
           {
              //写入回收站
               sqltxt="Insert into [company_del]([cbh],[company],[sbh],[sales],[getdate],[days],[outdate],[htcode],[stype],[status],[cnum]) "+
                       " SELECT [cbh],[company],[sbh],[sales],[getdate],[days],[outdate],[htcode],[stype],[status],[cnum]"+
                        " FROM [company_fav] where cbh ='" + dt.Rows[i][val].ToString().Trim() + "' ";
              // sqltxt = "delete "+tabtype+" where "+val+" ='" + dt.Rows[i][val].ToString().Trim() + "' ";
               DAL.DbHelperSQL.ExecuteNonQuery(sqltxt);

               sqltxt = " delete company_fav where cbh ='" + dt.Rows[i][val].ToString().Trim() + "' ";

               DAL.DbHelperSQL.ExecuteNonQuery(sqltxt);
           }

       }


       public static string intosales(string sty, string compid, string compname, string sbh, string sname,string ty)
       {
           string val = "";
           String[] strArr = getsalesnum(sty);
           string maxnum = strArr[0].ToString(); //最大数
           string maxday = strArr[1].ToString(); //保留天数
           DataTable d = BLL.Bllbase.get_table("company_fav", " sbh='" + sbh + "'  and LEFT(cbh,2)='" + ty + "' ");
           if (d.Rows.Count > int.Parse(maxnum))
           {
               val = "收藏夹已满，不能添加或者收藏新的" + sty + "，请删除后在添加";
           }
           else
           {

               string sqltxt = "insert into [company_fav] values(N'" + compid +  //公司ID
                                "',N'" + compname + //公司名称
                                "',N'" + sbh +   //员工编号
                                "',N'" + sname +  //员工姓名
                                "',getdate()," +  //收藏时间
                                maxday +  //保留天数
                                ",dateadd(DAY," + maxday + ",getdate()),'0',N'" + sty + "',0,0)";

               DAL.DbHelperSQL.ExecuteNonQuery(sqltxt);
               val = "添加成功";
           }
           return val;
       }

       /// <summary>
       /// 收藏企业
       /// </summary>
       /// <param name="sty">类型</param>
       /// <param name="compid">公司ID</param>
       /// <param name="sbh">编号</param>
       /// <param name="sname">名称</param>
       /// <returns></returns>
       public static string getsales(string sty, string complist, string sbh, string sname,string ty)
       {
           string val = "";
           String[] strArr = getsalesnum(sty);
           string maxnum = strArr[0].ToString(); //最大数
           string maxday = strArr[1].ToString(); //保留天数
           string sqltxt = "";
           string compbh="", compname="";
           string tab="";

           DataTable d ;
           d  = BLL.Bllbase.get_table("company_fav", "sbh='" + sbh + "' and LEFT(cbh,2)='"+ty+"'");
        
           if (d.Rows.Count >= int.Parse(maxnum))
           {
               val = "收藏夹已满，不能添加或者收藏新的" + sty + "，请取消已收藏信息后在收藏";
           }
           else
           {
               if (sty == "企业") tab = "company";
               if (sty == "学校") tab = "school";
               if (sty == "中介") tab = "zj";

               if (tab == "") tab = "company";

               DataTable dt = BLL.Bllbase.get_table(tab, " sid in (" + complist + ")");
               
               int tval=dt.Rows.Count;

               //如果要收藏数，大于剩于收藏数(50－当前记录数)
               if (dt.Rows.Count>(int.Parse(maxnum)-d.Rows.Count))
                   tval=int.Parse(maxnum)-d.Rows.Count;

               for (int i = 0; i < tval; i++)
               {
                   
                   if (tab == "company")
                   {
                       compbh = dt.Rows[i]["cbh"].ToString().Trim();
                       compname = dt.Rows[i]["cname"].ToString().Trim();
                   }
                   if (tab == "school")
                   {
                       compbh = dt.Rows[i]["schbh"].ToString().Trim();
                       compname = dt.Rows[i]["schname"].ToString().Trim();
                   }
                   if (tab == "zj")
                   {
                       compbh = dt.Rows[i]["zbh"].ToString().Trim();
                       compname = dt.Rows[i]["zname"].ToString().Trim();
                   }

                   sqltxt = "insert into [company_fav] values(N'" + compbh +  //公司ID
                                    "',N'" + compname + //公司名称
                                    "',N'" + sbh +   //员工编号
                                    "',N'" + sname +  //员工姓名
                                    "',getdate()," +  //收藏时间
                                    maxday +  //保留天数
                                    ",dateadd(DAY," + maxday + ",getdate()),'0',N'" + sty + "',0,0)";

                   DAL.DbHelperSQL.ExecuteNonQuery(sqltxt);


                   sqltxt = " delete company_del where cbh ='" + compbh + "' ";
                   DAL.DbHelperSQL.ExecuteNonQuery(sqltxt);
               }

            val = "收藏成功";

         }

           return val;

       }

    }
}
